"use client"; import { PromotionRep } from "@/api/home"; import Box from "@/components/Box"; import { CenterPopup } from "antd-mobile"; import { FC, useEffect, useState } from "react"; import dayjs from "dayjs"; import { Pagination } from "swiper/modules"; import { Swiper, SwiperSlide } from "swiper/react"; interface Props { data?: PromotionRep[]; type?: 1 | 2; // 每天只弹一次 / 每次登录都弹 } const HomePromotion: FC = (props) => { const { data, type } = props; const [visible, setVisible] = useState(false); useEffect(() => { const isClosePromotion = sessionStorage.getItem("isClosePromotion"); // 如果 type 为 1 && 有isNow为true 表示已经弹出, const shouldShowPromotion = () => { // 如果不等于1而数据又是时间,清除 if (type !== 1 && dayjs().isSame(isClosePromotion, "days")) { sessionStorage.removeItem("isClosePromotion"); return true; } if (type === 1) { return !dayjs().isSame(isClosePromotion, "days"); } else if (type === 2) { return !isClosePromotion; } return false; }; let flag = shouldShowPromotion(); setVisible(flag); }, []); const closeHandler = () => { setVisible(false); if (type === 1) { sessionStorage.setItem("isClosePromotion", dayjs().format("YYYY-MM-DD")); } if (type === 2) { sessionStorage.setItem("isClosePromotion", "true"); } }; if (data && data.length === 0) return null; return (
{data?.map((promotion, index) => (
{promotion.content.title}
{promotion.content.title}
))}
); }; export default HomePromotion;